package com.weigee.ktv.view.fragment

import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.linde.library.util.DisplayUtil
import com.linde.library.util.ListUtil
import com.weigee.ktv.R
import com.weigee.ktv.presenter.ApplySupermarketOrderRoomPresenter
import com.weigee.ktv.view.interfaces.ApplySupermarketOrderRoomView
import kotlinx.android.synthetic.main.wrap_recycler_refresh.*

/**
 * Created by linde on 2017/6/3.
 * LinDe
 */
class ApplySupermarketOrderRoomFragment : BaseFragment(), ApplySupermarketOrderRoomView {
    var presenter: ApplySupermarketOrderRoomPresenter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        presenter = ApplySupermarketOrderRoomPresenter(this)
        presenter?.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater?.inflate(R.layout.wrap_recycler_refresh, container, false) ?: super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ListUtil.initTwinklingRefreshLayout(refresh_Layout, object : RefreshListenerAdapter() {
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                super.onRefresh(refreshLayout)
                presenter?.loadRoomData()
            }

            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                super.onLoadMore(refreshLayout)
                presenter?.loadRoomData(true)
            }
        })
        val manager = GridLayoutManager(activity, 3)
        ListUtil.initRecyclerView(recycler_view, presenter?.adapter, manager)
        val padding = DisplayUtil.dip2px(activity, 8F)
        DisplayUtil.addPadding(recycler_view, padding, padding, padding, padding)
    }

    override fun taskPreview() {
    }

    override fun taskCompletely() {
    }

    override fun onLoadRoomDataCompletely(moreRoomData: Boolean) {
        if (moreRoomData) refresh_Layout.finishLoadmore() else refresh_Layout.finishRefreshing()
    }
}